﻿using Serial.ViewModels;
using System.IO.Ports;
using System.Windows;
using System.Windows.Controls;

namespace Serial.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
           
        }
          //波特率设置
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var vm = DataContext as MainWindowViewModel;
            var selectedText = (sender as ComboBox).SelectedValue?.ToString().Replace("System.Windows.Controls.ComboBoxItem: ","");

            vm._portHelper.SerialPortObject.BaudRate=int.Parse(selectedText);
            
        }
        //奇偶校验
        private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            var vm = DataContext as MainWindowViewModel;
            var selectedText = (sender as ComboBox).SelectedValue?.ToString().Replace("System.Windows.Controls.ComboBoxItem: ","");
            if (selectedText == "EVEN")
            {
                vm._portHelper.SerialPortObject.Parity=Parity.Even;
            }
            else if(selectedText == "NONE")
            {
                vm._portHelper.SerialPortObject.Parity=Parity.None;
            }
             else if(selectedText == "ODD")
            {
                vm._portHelper.SerialPortObject.Parity=Parity.Odd;
            }
        }
        //数据位
        private void ComboBox_SelectionChanged_2(object sender, SelectionChangedEventArgs e)
        {
            var vm = DataContext as MainWindowViewModel;
            var selectedText = (sender as ComboBox).SelectedValue?.ToString().Replace("System.Windows.Controls.ComboBoxItem: ","");
            vm._portHelper.SerialPortObject.DataBits=int.Parse(selectedText);
        }
        //停止位
        private void ComboBox_SelectionChanged_3(object sender, SelectionChangedEventArgs e)
        {
            var vm = DataContext as MainWindowViewModel;
            var selectedText = (sender as ComboBox).SelectedValue?.ToString().Replace("System.Windows.Controls.ComboBoxItem: ","");
            if (selectedText == "1")
            {
                vm._portHelper.SerialPortObject.StopBits = StopBits.One;
            }
             else if (selectedText == "2")
            {
                vm._portHelper.SerialPortObject.StopBits = StopBits.Two;
            }
           
        }
    }
}
